//
//  UIScrollView+RNOLEmptyDataSet.m
//  RongNiuOnline
//
//  Created by apple on 2018/5/6.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "UIScrollView+RNOLEmptyDataSet.h"
#import <objc/runtime.h>


@implementation UIScrollView (RNOLEmptyDataSet)

#pragma mark - Getter Setter
- (dispatch_block_t)clickEmptyViewBlock{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setClickEmptyViewBlock:(dispatch_block_t)clickEmptyViewBlock{
    objc_setAssociatedObject(self, @selector(clickEmptyViewBlock), clickEmptyViewBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (dispatch_block_t)clickButtonBlock{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setClickButtonBlock:(dispatch_block_t)clickButtonBlock{
    objc_setAssociatedObject(self, @selector(clickButtonBlock), clickButtonBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)buttonText{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setButtonText:(NSString *)buttonText{
    objc_setAssociatedObject(self, @selector(buttonText), buttonText, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)emptyText{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setEmptyText:(NSString *)emptyText{
    objc_setAssociatedObject(self, @selector(emptyText), emptyText, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGFloat)offset{
    NSNumber *number = objc_getAssociatedObject(self, _cmd);
    return number.floatValue;
}

- (void)setOffset:(CGFloat)offset{
    NSNumber *number = [NSNumber numberWithDouble:offset];
    objc_setAssociatedObject(self, @selector(offset), number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIImage *)emptyImage{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setEmptyImage:(UIImage *)emptyImage{
    objc_setAssociatedObject(self,@selector(emptyImage), emptyImage, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (BOOL)emptyViewScrollable{
    BOOL enable = [objc_getAssociatedObject(self, _cmd) boolValue];
    return enable;
}

- (void)setEmptyViewScrollable:(BOOL)emptyViewScrollable{
    objc_setAssociatedObject(self, @selector(emptyViewScrollable), @(emptyViewScrollable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}



#pragma mark- 添加空白数据提示视图
- (void)addEmptyViewWithTipText:(NSString *)tipText
                 tapClickAction:(dispatch_block_t)block{
    [self addEmptyViewWithTipText:tipText buttonText:nil localImage:nil verticalOffset:0 tapClickAction:block buttonAction: nil];
}

- (void)addEmptyViewWithButtonText:(NSString *)buttonText
                 buttonClickAction:(dispatch_block_t)buttonClickBlock{
    [self addEmptyViewWithTipText:nil buttonText:buttonText localImage:nil verticalOffset:0 tapClickAction:nil buttonAction: buttonClickBlock];
}


- (void)addEmptyViewWithTipText:(NSString *)tipText
                     localImage:(NSString *)loacalImgName
                 tapClickAction:(dispatch_block_t)block{
    [self addEmptyViewWithTipText:tipText buttonText:nil localImage:loacalImgName verticalOffset:0 tapClickAction:block buttonAction:nil];

}

- (void)addEmptyViewWithTipText:(NSString *)tipText
                     localImage:(NSString *)loacalImgName
                 verticalOffset:(CGFloat)offset
                 tapClickAction:(dispatch_block_t)block{
    [self addEmptyViewWithTipText:tipText buttonText:nil localImage:loacalImgName verticalOffset:offset tapClickAction:block buttonAction:nil];
}

- (void)addEmptyViewWithTipText:(NSString *)tipText
                     buttonText:(NSString *)buttonTitle
                     localImage:(NSString *)loacalImgName
                 verticalOffset:(CGFloat)offset
                 tapClickAction:(dispatch_block_t)emptyViewClickBlock
                   buttonAction:(dispatch_block_t)buttonBlock{
    self.emptyText = tipText;
    self.buttonText = buttonTitle;
    self.emptyImage = loacalImgName? [UIImage imageNamed: loacalImgName] : nil;
    self.offset = offset;
    self.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
    self.clickEmptyViewBlock = emptyViewClickBlock;
    self.clickButtonBlock = buttonBlock;
}


#pragma mark - DZNEmptyDataSetSource
// 空白界面的标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = self.emptyText? : @"暂无数据";
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    paragraph.lineSpacing = 5;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                 NSParagraphStyleAttributeName: paragraph};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    [attributes setObject: KFontSize(18) forKey:NSFontAttributeName];
    [attributes setObject:kAppMainColor forKey:NSForegroundColorAttributeName];
    return [[NSAttributedString alloc] initWithString: self.buttonText? : @"重新加载" attributes:attributes];
}

//- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView{
//    UIView *tempView = [self customEmptyDataView];
//    return tempView;
//}

// 空白页的图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return self.emptyImage?:[UIImage imageNamed:@"rnol_nonedata_ufo"];
}

//是否允许滚动，默认NO
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return self.emptyViewScrollable? : NO;
}

// 垂直偏移量
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    return self.offset;
}

#pragma mark - DZNEmptyDataSetDelegate
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    !self.clickEmptyViewBlock? : self.clickEmptyViewBlock();
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button{
    !self.clickButtonBlock? : self.clickButtonBlock();
}

- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView {
    self.contentOffset = CGPointZero;
}
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIColor whiteColor];
}



@end
